﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace AgLux
{
    /// <summary>
    /// //
    /// </summary>

    public class Task : Component
    {
        protected TextBlock _headline, _footline;
        protected Canvas _button;
        protected TextBlock _buttonText;

        protected List<Question> _questions;
        protected int _questionIndex, _correctAnswers;

        /// <summary>
        /// //
        /// </summary>

        /// <param name="questions">
        /// //
        /// </param>
        
        public Task(List<Question> questions)
            : base()
        {
            _headline = (TextBlock)_resource.FindName("headline");
            _footline = (TextBlock)_resource.FindName("footline");
            _button = (Canvas)_resource.FindName("button");
            _buttonText = (TextBlock)_resource.FindName("buttonText");

            _questions = questions;
            _questionIndex = _correctAnswers = 0;

            _headline.Visibility = Visibility.Visible;

            _footline.Text = "Correct answers: " + _correctAnswers;
            _footline.Visibility = Visibility.Visible;

            _button.Visibility = Visibility.Visible;
            _button.MouseLeftButtonDown += new MouseEventHandler(ButtonPressed);

            ShowQuestion(_questionIndex);
            _screen.SetValue(Canvas.TopProperty, _headline.ActualHeight + 10);
        }

        /// <summary>
        /// //
        /// </summary>

        /// <param name="canvasWidth">
        /// //
        /// </param>

        public override void FitCanvas(double canvasWidth)
        {
            Width = ScreenWidth = canvasWidth;

            foreach (Question question in _questions)
            {
                if (Height < question.Height)
                {
                    Height = question.Height;
                }
            }

            Height += _headline.ActualHeight + _button.Height + _footline.ActualHeight + 30;
            ScreenHeight = _questions[_questionIndex].Height;
        }

        /// <summary>
        /// //
        /// </summary>

        /// <param name="value">
        /// //
        /// </param>

        protected override double ScreenWidth
        {
            set
            {
                _screen.Width = _headline.Width = _footline.Width = value;

                foreach (Question question in _questions)
                {
                    question.FitCanvas(value);
                }
            }
        }

        /// <summary>
        /// //
        /// </summary>

        /// <param name="value">
        /// //
        /// </param>

        protected override double ScreenHeight
        {
            set
            {
                _screen.Height = value;

                _footline.SetValue(Canvas.TopProperty, _headline.ActualHeight + value + 20);
                _button.SetValue(Canvas.TopProperty, _headline.ActualHeight + value + _footline.ActualHeight + 30);
             }
        }

        protected override void SizeStandard(object sender, System.Windows.Input.MouseEventArgs args)
        {

        }


        /// <summary>
        /// //
        /// </summary>

        /// <param name="text">
        /// //
        /// </param>

        protected virtual void SetButton(string text)
        {
            _buttonText.Text = text;
            _buttonText.SetValue(Canvas.LeftProperty, _button.Width / 2 - _buttonText.ActualWidth / 2);
        }

        /// <summary>
        /// //
        /// </summary>

        /// <param name="questions">
        /// //
        /// </param>

        protected virtual void ShowQuestion(int index)
        {
            _questionIndex = index;
            _screen.Children.Clear();

            if (_questionIndex < _questions.Count)
            {
                _headline.Text = "Question " + (_questionIndex + 1) + " of " + _questions.Count + ":";
                _screen.Children.Add(_questions[_questionIndex]);
                
                ScreenHeight = _questions[_questionIndex].Height;
                SetButton("Submit");
            }

            else
            {
                _headline.Text = "Correct answers: " + _correctAnswers + " of " + _questions.Count;

                _footline.Visibility = Visibility.Collapsed;
                _button.Visibility = Visibility.Collapsed;
            }
        }

        /// <summary>
        /// //
        /// </summary>

        /// <param name="sender">
        /// //
        /// </param>

        /// <param name="args">
        /// //
        /// </param>

        protected virtual void ButtonPressed(object sender, MouseEventArgs args)
        {
            if (_questions[_questionIndex].IsCorrected)
            {
                ShowQuestion(_questionIndex + 1);
            }

            else if (_questions[_questionIndex].IsAnswered)
            {
                if (_questions[_questionIndex].Correct())
                {
                    _correctAnswers++;
                    _footline.Text = "Correct answers: " + _correctAnswers;
                }

                SetButton("Next");
            }
        }
    }
}
